package com.zzyl;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class redisTest {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Test
    public void test() {
        System.out.println(redisTemplate);
    }

    @Test
    public void test1() {
        redisTemplate.opsForValue().set("name", "huangshuxin");
        System.out.println(redisTemplate.opsForValue().get("name"));


        // 设置过期时间
        redisTemplate.opsForValue().set("token", "123456",20, TimeUnit.SECONDS);
        System.out.println(redisTemplate.opsForValue().get("token"));

        redisTemplate.opsForValue().setIfAbsent("lack","1234",5,TimeUnit.MINUTES);
        redisTemplate.opsForValue().setIfAbsent("lack","1556",5,TimeUnit.MINUTES);

    }

    @Test
    public void testHash() {
        // hash
        redisTemplate.opsForHash().put("user","name","huangshuxin");
        redisTemplate.opsForHash().put("user","age","18");

        // 获取大key和小key的值
        System.out.println(redisTemplate.opsForHash().get("user","name"));

        // 根据大key获取所有值
        System.out.println(redisTemplate.opsForHash().values("user"));

        // 根据大key获取所有小key和值

        System.out.println(redisTemplate.opsForHash().keys("user"));

        //删除小key和值
        redisTemplate.opsForHash().delete("user","age");
    }

    @Test
    public void testList() {
        // 插入多个值[a,b,c]
        redisTemplate.opsForList().leftPushAll("list","a","b","c");
        //在列表左边插入一个值d
        redisTemplate.opsForList().leftPush("list","d");
        //获取所有数据
        List<String> list = redisTemplate.opsForList().range("list", 0, -1);
        //从左边弹出一个，并获取值，然后删除
        String leftPop = redisTemplate.opsForList().leftPop("list");
        System.out.println(leftPop);
        //获取列表的长度
        long size = redisTemplate.opsForList().size("list");
    }

    @Test
    public void testSet() {
        //添加数据
        redisTemplate.opsForSet().add("set","a","b","c","d","e");
        redisTemplate.opsForSet().add("set2","a","b","c","x","y");

        //获取集合中的所有成员
        Set<String> set = redisTemplate.opsForSet().members("set");
        System.out.println(set);

        //获取集合大小
        long size = redisTemplate.opsForSet().size("set");
        System.out.println(size);

        //交集
        Set<String> intersection = redisTemplate.opsForSet().intersect("set", "set2");
        System.out.println(intersection);

        //并集
        Set<String> union = redisTemplate.opsForSet().union("set", "set2");
        System.out.println(union);
    }

    @Test
    public void testZSet() {

        // 添加数据
        redisTemplate.opsForZSet().add("zset","a",1);
        redisTemplate.opsForZSet().add("zset","b",2);
        redisTemplate.opsForZSet().add("zset","c",3);
        redisTemplate.opsForZSet().add("zset","d",4);

        // 获取集合中的所有成员
        Set<String> zset = redisTemplate.opsForZSet().range("zset", 0, -1);
        System.out.println(zset);

        //给a成员的分数添加10
        redisTemplate.opsForZSet().incrementScore("zset","a",0);
        System.out.println(redisTemplate.opsForZSet().range("zset", 0, -1));

        //删除a，b两个成员
        redisTemplate.opsForZSet().remove("zset","a","b");
        System.out.println(redisTemplate.opsForZSet().range("zset", 0, -1));
    }
}
